let utils = require('../../../../../utils/util.js');
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		teamInfo: '',
		introduce: '',
		formData: {
			companyProduct: '', //企业简介
			companyDemand: '', //产品
			companyRecruit: '', //需求
			companyProfile: '', //招聘

		},
		src: '',
		cuttype: null,
		teamMemberList: [], //当前团队成员
		showContacts: false,
		JointeamList:[],
		teamList:[],//所加入的团队
		visibilityTeamList:false
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		var cardId = options.cardId
		if (cardId) {
			this.setData({
				cardId: cardId,
			})
			this.cardByInfoType(cardId)
		}
		this.cardTeamInfo()
		this.teamMemberList()
		this.getJoinedTeamListByUserId()
	},
	onShow: function() {
		if (this.data.cuttype) {
			utils.personUpLoadImg(this.data.src)
				.then(res => {
					this.setData({
						['formData.companyBackgroundImg']: res
					})
				})
		}
	},
	// 获取名片信息
	cardByInfoType(cardId) {
		var data = {
			id: cardId,
			infoType: 4
		}
		utils.request(utils.personApi + '/personal/get/cardByInfoType', 'GET', data)
			.then(res => {
				if (res.code == 0) {
					this.setData({
						formData: res.data.info
					})
				}
			})
	},
	// 导入团队信息
	import_info(){
		var userTeamList = this.data.JointeamList
		if(userTeamList.length > 1){
			
			var teamList = userTeamList.map(item => {
				return {
					title: item.teamName,
					id: item.teamId
				}
			})
			
			this.setData({
				visibilityTeamList:true,
				teamList
			})
		}
	},
	// 从个人团队弹框选择名片
	getChooseTeamCard(e) {
		var index = e.detail.index
		var teamId = this.data.teamList[index].id
		this.companyprofilefromteam(teamId)
		
	},
	// 将团队的企业信息导入到个人名片企业信息中
	companyprofilefromteam(teamId){
		var data = {
			teamId,
			cardId:this.data.cardId
		}
		utils.request(utils.personApi + '/personal/import/companyprofilefromteam', 'POST',data)
			.then(res => {
				if (res.code == 0) {
					this.cardByInfoType(this.data.cardId)
					wx.showToast({
						title: '导入成功',
						icon: 'success',
						duration: 2000
					})
				}
			})
	},
	// 通过用户userId获取所加入的团队
	getJoinedTeamListByUserId(){
		utils.request(utils.personApi + '/personal/getJoinedTeamListByUserId', 'GET', {})
			.then(res => {
				if (res.code == 0) {
					this.setData({
						JointeamList: res.data.teamList
					})
				}
			})
	},
	// 获取团队企业简介
	cardTeamInfo() {
		utils.request(utils.personApi + '/personal/company/getProfile', 'GET')
			.then(res => {
				if (res.code == 0) {
					this.setData({
						formData: res.data
					})
				}
				// 隐藏spin
				setTimeout(() => {
					this.setData({
						spinShow: false
					})
				}, 100)
			})
	},
	// 监听企业名称输入
	companyNameHandle(e) {
		this.setData({
			['formData.companyName']: e.detail.value,
		})
	},
	// 监听企业网址输入
	companyWebUrlHandle(e) {
		this.setData({
			['formData.companyWebUrl']: e.detail.value,
		})
	},
	// 监听联系人输入
	userCardInfoNameHandle(e) {
		this.setData({
			['formData.userCardInfos.cardName']: e.detail.value,
		})
	},
	// 获取联系人信息
	getuserCardInfos(e) {
		const {
			usercardinfos
		} = e.currentTarget.dataset
		this.setData({
			['formData.userCardInfos']: usercardinfos,
			showContacts: false
		})
	},
	// 从本地相册选择图片或使用相机拍照。
	chooseImage() {
		var self = this;
		wx.chooseImage({
			count: 9,
			sizeType: ['original', 'compressed'],
			sourceType: ['album', 'camera'],
			success(res) {
				const tempFilePaths = res.tempFilePaths
				var totalFile = self.data.tempFilePaths.concat(res.tempFilePaths)
				self.setData({
					tempFilePaths: res.tempFilePaths
				})

				self.uploadimg({
					path: tempFilePaths
				})

			}
		})
	},
	// 获取团队成员信息
	teamMemberList() {
		var data = {
			status: 2
		}
		return new Promise((resolve, reject) => {
			utils.request(utils.personApi + '/team/find/teamlist', 'GET', data)
				.then(res => {
					if (res.code == 0) {
						this.setData({
							teamMemberList: res.data.teamMemberList
						})
					}
				})
		})
	},
	// 显示团队成员
	showContactsHandle() {
		this.setData({
			showContacts: true
		})
	},
	// 隐藏团队成员
	closeContactsHandle() {
		this.setData({
			showContacts: false
		})
	},
	// 提交信息
	formSubmit(e) {
		var formData = this.data.formData
		formData.cardId = this.data.cardId
		if (!formData.companyName) {
			wx.showToast({
				title: '请填写企业名称',
				icon: 'none',
				duration: 2000
			})
			return
		} else if (!formData.companyWebUrl) {
			wx.showToast({
				title: '请填写企业网址',
				icon: 'none',
				duration: 2000
			})
			return
		}

		utils.request(utils.personApi + '/personal/update/card/company/profile', 'POST', formData)
			.then(res => {
				if (res.code == 0) {
					wx.showToast({
						title: '资料提交成功',
						icon: 'none',
						duration: 2000
					})
					wx.removeStorageSync('companyProfile')
					wx.removeStorageSync('companyRecruit')
					wx.removeStorageSync('companyDemand')
					wx.removeStorageSync('companyProduct')
				} else {
					wx.showToast({
						title: res.error,
						icon: 'none',
						duration: 2000
					})
				}
			})
	},

	toNavigator(e) {
		var {
			url
		} = e.currentTarget.dataset
		utils.navigateTo({
			url: url
		})
	},

	chooseIMG() {
		wx.navigateTo({
			url: '/pages/admin/team/mine/cutFace/index?cuttype=1',
		})
	},
	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	}
})
